package top.catchcat.blogs.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;
import top.catchcat.blogs.dao.blogs.Blog;
import top.catchcat.blogs.dao.blogs.BlogQuery;

import java.util.List;

@Repository
public interface BlogMapper {
    @Insert("INSERT INTO blog VALUES (NULL, #{title}, #{content}, #{published}, NOW(), NOW(), 0, 0, #{tags}, #{description}, #{userid})")
    boolean insertBlog(Blog blog);

    @Update("UPDATE blog SET title = #{title}, content = #{content}, published = #{published}, updateTime = NOW(), tags = #{tags}, description = #{description} WHERE id = #{id}")
    boolean updateBlog(Blog blog);

    @Delete("DELETE FROM blog WHERE id = #{id}")
    boolean deleteBlog(Long  id);


    @Select("SELECT * FROM blog WHERE id = #{id}")
    @Result(column = "user_id",property = "user",
            one = @One(select = "top.catchcat.blogs.mapper.UserMapper.getUserById",fetchType = FetchType.EAGER))
    @Result(column = "user_id",property = "user_id")
    Blog getBlogById(Long id);

    List<Blog> getAllBlogsBySearch(BlogQuery blogQuery);

    Integer getCountBySearch();

    @Select("SELECT * FROM blog WHERE user_id = #{id}")
    @Result(column = "user_id",property = "user",
            one = @One(select = "top.catchcat.blogs.mapper.UserMapper.getUserById",fetchType = FetchType.EAGER))
    @Result(column = "user_id",property = "user_id")
    List<Blog> getAllBlogsByUserId(Long id);

}
